﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using backoffice.Data;
using System.Linq.Expressions;

namespace backoffice.Business
{
    public class BusinessQuestion
    {
        #region Create
        public static void createQuestion(Question q)
        {
            try
            {
                dataQuestion.createQuestion(q);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating question");
            }
        }

        public static void createQuestion(User u, Pathology p, Plant pl, Method m, String text)
        {
            try
            {
                Question q = new Question();
                q.UserID = u.UserID;
                if (p != null)  q.PathologyID = p.PathologyID;
                if (pl != null) q.PlantID = pl.PlantID;
                if (m != null)  q.MethodID = m.MethodID;
                q.Text = text;

                dataQuestion.createQuestion(q);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error creating question");
            }
        }
        #endregion

        #region Get
        public static Question getQuestion(Guid guid)
        {
            try
            {
                return dataQuestion.getQuestion(guid);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question");
            }
        }

        public static List<Question> getQuestionsByUser(User u)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.UserID == u.UserID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by user");
            }
        }

        public static List<Question> getQuestionsByUserAndPlant(User u, Plant p)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.UserID == u.UserID) && (q.PlantID == p.PlantID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by user and plant");
            }
        }

        public static List<Question> getQuestionsByUserAndPathology(User u, Pathology p)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.UserID == u.UserID) && (q.PathologyID == p.PathologyID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by user and pathology");
            }
        }

        public static List<Question> getQuestionsByUserAndMethod(User u, Method m)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.UserID == u.UserID) && (q.MethodID == m.MethodID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by user and method");
            }
        }

        public static List<Question> getQuestionsByPathology(Pathology p)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.PathologyID == p.PathologyID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by pathology");
            }
        }

        public static List<Question> getQuestionsByPlant(Plant p)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.PlantID == p.PlantID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by plant");
            }
        }

        public static List<Question> getQuestionsByMethod(Method m)
        {
            try
            {
                Expression<Func<Question, bool>> whereClause = (Question q) => (q.MethodID == m.MethodID);

                return dataQuestion.getQuestions(whereClause).ToList();
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error getting question by method"); ;
            }
        }
        #endregion

        #region Set
        public static Boolean changeQuestion(Question q)
        {
            try
            {
                return dataQuestion.changeQuestion(q);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error changing question");
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteQuestion(Question q)
        {
            try
            {
                return dataQuestion.deleteQuestion(q);
            }
            catch (Exception e)
            {
                Messages.AddDebug(e.Message);
                throw new Exception("Error deleting question");
            }
        }
        #endregion
    }
}
